package thread;

public class ThreadJoin {

    public static void main(String[] args) throws InterruptedException {
        Thread t1=new Thread(()-> {
            for(int i=0;i<3;i++){
                System.out.println("这是线程t1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("线程t1结束");
        });

        Thread t2=new Thread(()-> {
            for(int i=0;i<3;i++){
                System.out.println("这是线程t2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("线程t2结束");
        });
        t1.start();
        t2.start();
        System.out.println("main开始等待t1,t2");
        t1.join();
        t2.join();
        System.out.println("main等待结束");
    }
    public static void main1(String[] args) throws InterruptedException {
        Thread thread=new Thread(()-> {
           for(int i=0;i<3;i++){
               System.out.println("这是线程thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
            System.out.println("线程thread结束");
        });
        thread.start();
        Thread.sleep(4000);//让主线程先沉睡4秒,在此过程中,足够thread线程执行完毕了
        //让主线程等待thread线程
        System.out.println("main线程开始等待");
        try {
            thread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("main线程等待结束");
    }
}
